﻿using PrinterMonitorSvc.Common;
using PrinterMonitorSvc.Core;
using PrinterMonitorSvc.Ext;
using PrinterMonitorSvc.Model;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PrinterMonitorSvc
{
    public partial class MainForm : Form
    {
        private static bool _isClose = false;

        #region 窗体控件加载

        /// <summary>
        /// 构造方法
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载，启动更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            if (!WinHelper.IsAdministrator())
            {
                MessageBox.Show("Please run with administrator privileges");
                this.Close();
                return;
            }

            if (WinHelper.IsPortOccuped(6789, PortTypeEnum.TCP))
            {
                MessageBox.Show("Port 6789 is occupied. Please close the occupied process and try again");
                this.Close();
                return;
            }
            //自动更新
            SystemHelper.AutoUpdate();
            //启动
            Start();

        }

        /// <summary>
        /// 启动
        /// </summary>
        private void Start()
        {
            if (_isClose)
            {
                return;
            }

            #region 取消自启动
            try
            {
                WinHelper.AutoStartByRegedit(true, "PrinterMonitorSvc", Process.GetCurrentProcess().MainModule.FileName);
            }
            catch (Exception ex)
            {
                MonitorContext.LogError($"程序自启动添加注册表异常：{ex}");
            }
            #endregion

            MonitorContext.Init();

            this.notifyIcon.Visible = true;
            //弹气泡/通知框提示
            ShowTip("Printer Monitor Is Running!");
        }

        #region 右键菜单
        /// <summary>
        /// 退出程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void closeitem_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Process thisproc in Process.GetProcessesByName(SystemConstant.APP_NAME))
                {
                    if (!thisproc.CloseMainWindow())
                    {
                        thisproc.Kill();
                    }
                }
            }
            catch (Exception Exc)
            {
                MessageBox.Show(Exc.Message);
            }
        }

        /// <summary>
        /// 当前版本号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void versionitem_Click(object sender, EventArgs e)
        {
            ShowTip($"Current Version Is {Application.ProductVersion}");
        }

        /// <summary>
        /// 打开日志目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void logitem_Click(object sender, EventArgs e)
        {
            try
            {
                string logPath = $"{Environment.CurrentDirectory}\\log";
                if (!Directory.Exists(logPath))
                {
                    ShowTip("No Log file");
                    return;
                }
                Process.Start("explorer.exe", logPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"打开日志目录异常：{ex}");
            }
        }
        /// <summary>
        /// 打开文件目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fileitem_Click(object sender, EventArgs e)
        {
            try
            {
                string filePath = Path.GetTempPath();
                if (!Directory.Exists(filePath))
                {
                    ShowTip("No files");
                    return;
                }
                Process.Start("explorer.exe", filePath);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"打开文件目录异常：{ex}");
            }
        }

        /// <summary>
        /// 打开配置文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void configitem_Click(object sender, EventArgs e)
        {
            try
            {
                string filePath = $"{Environment.CurrentDirectory}\\{SystemConstant.CONFIG_NAME}";
                if (!File.Exists(filePath))
                {
                    ShowTip("No configuration file");
                    return;
                }
                Process.Start("explorer.exe", filePath);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"打开配置文件异常：{ex}");
            }
        }

        /// <summary>
        /// 打开执行文件目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exeitem_Click(object sender, EventArgs e)
        {
            try
            {
                string filePath = Process.GetCurrentProcess().MainModule.FileName;
                if (!File.Exists(filePath))
                {
                    ShowTip("No Exe file");
                    return;
                }
                Process.Start("explorer.exe", new FileInfo(filePath).DirectoryName);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"打开执行文件目录：{ex}");
            }
        }

        #endregion

        /// <summary>
        /// 窗口关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            _isClose = true;
        }

        #endregion

        /// <summary>
        /// 系统提示
        /// </summary>
        /// <param name="msg"></param>
        private void ShowTip(string msg)
        {
            //弹气泡/通知框提示
            this.notifyIcon.ShowBalloonTip(20, SystemConstant.APP_NAME, msg, ToolTipIcon.Info);
        }
    }
}
